import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

///图片封装
class AppImg extends StatelessWidget {
  ///图片路径
  final String url;

  ///宽度
  final double width;

  ///高度
  final double height;

  ///填充模式
  final BoxFit fit;

  const AppImg({
    Key key,
    @required this.url,
    this.width,
    this.height,
    this.fit = BoxFit.cover,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (url == null || url == '')
      return const Center(child: Text('null image'));

    //网络图片
    if (url.startsWith('http'))
      return CachedNetworkImage(
        imageUrl: url,
        width: width,
        height: height,
        fit: fit,
      );

    //设备本地图片
    if (url.startsWith('/'))
      return FadeInImage(
        image: FileImage(File(url)),
        placeholder: NetworkImage(url),
        width: width,
        height: height,
        fit: fit,
      );

    //应用内图片
    return Image.asset(
      url,
      width: width,
      height: height,
      fit: fit,
    );
  }
}
